//
//  YSOrderReviewItemView.m
//  Yueshi
//
//  Created by Northwan on 15/9/30.
//  Copyright © 2015年 悦食中国. All rights reserved.
//

#import "YSOrderReviewItemView.h"
#import "YSLevelView.h"
#import "SelectImgUtils.h"
#import "YSProgressImageView.h"

#define CELL_MAX_COUNT_PIC 4

@interface YSOrderReviewItemView()<YSLevelViewDelegate,SelectImgUtilsDelegate>
@property (strong, nonatomic) UIImageView *iconView;
@property (strong, nonatomic) UILabel *nameLabel;
@property (strong, nonatomic) UILabel *skuLabel;
@property (strong, nonatomic) YSLevelView *scoreView;
@property (strong, nonatomic) UIView *spliteView;

@end

@implementation YSOrderReviewItemView


-(instancetype)init{
    if(self = [super init]){
        UIImageView *iconView = [UIImageView new];
        [iconView setCornerRadius:5 borderWidth:.5 borderColor:HEXColor(@"#eaeae9")];
        [self addSubview:iconView];
        _iconView = iconView;
        
        UILabel *nameLabel = [UILabel new];
        nameLabel.font = SYS_FONT(NAMEFONT);
        nameLabel.textColor = publicTextColor;
        [self addSubview:nameLabel];
        _nameLabel = nameLabel;
        
        UILabel *skuLabel = [UILabel new];
        skuLabel.font = SYS_FONT(SKUFONT);
        skuLabel.textColor = HEXColor(@"#9e9e9e");
        [self addSubview:skuLabel];
        _skuLabel = skuLabel;
        
        YSLevelView *scoreView = [[YSLevelView alloc]initWithSourceType:ReviewSourceType];
        [scoreView setLevel:5];
        [scoreView setDelegate:self];
        [self addSubview:scoreView];
        _scoreView = scoreView;
        
        YSTextView *reviewTV = [[YSTextView alloc]init];
        [reviewTV setBackgroundColor:PUBLIC_BG];
        [reviewTV setFont:SYS_FONT(13)];
        [reviewTV setPlaceholder:@"快来尽情评价吧!"];
        [reviewTV setCornerRadius:5 borderWidth:.5 borderColor:HEXColor(@"#eaeae9")];
        [self addSubview:reviewTV];
        _reviewTV = reviewTV;
        
        UIButton *selectBtn = [UIButton new];
        [selectBtn addTarget:self action:@selector(selectPicture:) forControlEvents:UIControlEventTouchUpInside];
        [selectBtn setImage:IMG(@"btn_camera_normal") forState:UIControlStateNormal];
        [selectBtn setBackgroundColor:PUBLIC_BG];
        [selectBtn setCornerRadius:5 borderWidth:.5 borderColor:HEXColor(@"#eaeae9")];
        [self addSubview:selectBtn];
        _selectBtn = selectBtn;
        
        _photosView = [[YSPhotosView alloc]init];
        _photosView.isShowProgress = YES;
         [self addSubview:_photosView];
        
        UIView *spliteView = [[UIView alloc]init];
        [spliteView setBackgroundColor:PUBLIC_BG];
        [self addSubview:spliteView];
        _spliteView = spliteView;
    }
    return self;
}

-(void)tapLevelCallBack:(NSInteger)score{
    _reviewFrame.product.score = score;
}

-(void)selectPicture:(UIButton *)sender{
    if(_reviewFrame.product.photosImage.count < CELL_MAX_COUNT_PIC){
        SelectImgUtils *selectImgUtils = [SelectImgUtils shareInstance];
        [selectImgUtils selectImageWithCtrl:self.viewController];
        [selectImgUtils setDelegate:self];
        selectImgUtils.maxSelectCount = CELL_MAX_COUNT_PIC - _reviewFrame.product.photosImage.count
        ;
    }else{
        ShowTips(@"最多选择四张照片")
    }
}

-(void)selectImgUtilsImg:(UIImage *)img{
    YSSelectImageModel *modle = [YSSelectImageModel new];
    modle.image = img;
    modle.row = self.selectBtn.tag;
    modle.tag = _reviewFrame.product.photosImage.count;
    
    if([_delegate respondsToSelector:@selector(selectImgWithModel:)]){
        [_delegate selectImgWithModel:modle];
    }
}

-(void)setReviewFrame:(YSOrderReviewFrame *)reviewFrame{
    _reviewFrame = reviewFrame;
    YSCartProduct *product = reviewFrame.product;
    
    _iconView.frame = reviewFrame.iconFrame;
    [_iconView setImageURLStr:product.prodImageUrl];
    
    _nameLabel.frame = reviewFrame.nameFrame;
    [_nameLabel setText:product.prodName];
    
    _skuLabel.frame = reviewFrame.skuFrame;
    [_skuLabel setText:product.prodBuyLink];
    
    [_scoreView setX:reviewFrame.scoreX];
    [_scoreView setY:reviewFrame.scoreY];
    [_scoreView setLevel:product.score];
    
    _reviewTV.frame = reviewFrame.reviewFrame;
     [_reviewTV setText:product.comment];
    
    _selectBtn.frame = reviewFrame.selectFrame;
    
    if(product.photosImage.count){
        [_photosView setHidden:NO];
        _photosView.x = reviewFrame.photosX;
        _photosView.y = reviewFrame.photosY;
        [_photosView addPhotos:product.photosImage];
    }else{
        [product.photosImage removeAllObjects];
        [_photosView setHidden:YES];
    }
    
    _spliteView.frame = reviewFrame.spliteFrame;
    
    [self setFrame:reviewFrame.frame];
}

@end
